<template>
  <div>
    <el-dialog
      title="切换版本"
      :visible="visible"
      width="40%"
      @close="visible = false"
      :close-on-click-modal="false"
    >
      <el-table
        ref="singleTable"
        :data="selections"
        @current-change="handleCurrentChange"
        @selection-change="handleSelectionChange"
        style="width: 100%; height: 40vh"
        v-loading="loading"
      >
        <!-- <el-table-column type="selection" width="50"> </el-table-column> -->
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column property="activeVersion" label="版本编号" width="120">
        </el-table-column>
        <el-table-column property="name" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.activeVersion == value.activeVersion"
              >激活</span
            >
          </template>
        </el-table-column>
        <el-table-column property="name" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="edit(scope.row)"
              type="text"
              size="small"
              >编辑</el-button
            >
            <el-button
              v-if="scope.row.activeVersion != value.activeVersion"
              @click="updateActiveVersion(scope.row)"
              type="text"
              size="small"
              >切换</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirm" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <editDialog ref="editDialog"></editDialog>
  </div>
</template>

<script>
import editDialog from "./editDialog";

export default {
  components: { editDialog },
  props: {},
  watch: {
    visible(newVal, oldVal) {
      if (!newVal && oldVal) this.hide();
    },
  },
  model: {
    prop: "value",
    event: "change",
  },
  data() {
    return {
      loading: false,
      visible: false,
      defaultLabelWidth: "150px",
      form: {},
      rules: {},
      currentRow: null,
      selections: [],
      value: {},
      multipleSelection: [],
    };
  },
  methods: {
    confirm() {
      this.commit();
    },
    show(value) {
      debugger;
      this.visible = true;
      this.value = { ...value };
      this.loadData(this.value);
    },
    hide() {
      this.visible = false;
      this.form = {
        name: "",
      };
    },
    loadData(value) {
      this.loading = true;
      this.value = value;
      this.getAllFormDesignVersions(value.id).then(() => {
        this.loading = false;
      });
    },
    getAllFormDesignVersions(formDesignId) {
      return this.$store
        .dispatch("dynamicFormDesign/getAllFormDesignVersions", {
          formDesignId: formDesignId,
        })
        .then((res) => {
          console.log(res);
          this.selections = res;
        });
    },
    updateActiveVersion(versionInfo) {
      this.loading = true;
      this.$store
        .dispatch("dynamicFormDesign/setActiveVersion", {
          id: versionInfo.id,
          version: versionInfo.activeVersion,
        })
        .then((res) => {
          this.loading = false;
          this.value.activeVersion = versionInfo.activeVersion;
        });
    },
    commit() {
      this.$emit("ok-click", {});
      this.hide();
    },
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    handleSelectionChange(val) {
      debugger;
      this.multipleSelection = val;
    },
    edit(row) {
      // this.$router.push({
      //   path: "/workflowDesign/editPage",
      //   query: {
      //     id: row.id,
      //     version: row.activeVersion || 1,
      //   },
      // });

      this.$refs.editDialog.show({
        id: row.id,
        version: row.activeVersion || 1,
      });
    },
  },
};
</script>

<style></style>
